package com.android.hw.projectmenu;
import com.android.hw.projectmenu.R;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.KeyEvent;

public class MotionSenorTest extends Activity {
	
	private double Φ , ρ;
	private String TAG = "MotionSenorTest";

	public MotionSenorTest() {

	}

	private void initAllControl() {

		ms_tv_XYZ[0] = (TextView) findViewById(R.id.ms_tv_x);
		ms_tv_XYZ[1] = (TextView) findViewById(R.id.ms_tv_y);
		ms_tv_XYZ[2] = (TextView) findViewById(R.id.ms_tv_z);
		ms_tv_XYZ[3] = (TextView) findViewById(R.id.text1);
		ms_tv_XYZ[4] = (TextView) findViewById(R.id.text2);

	}

	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		mSensorManager = (SensorManager) getSystemService("sensor");
		mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		ms_tv_XYZ = new TextView[5];
		setContentView(R.layout.test_motionsenor);
		initAllControl();
	}

	protected void onResume() {
		super.onResume();
		boolean flag = mSensorManager.registerListener(mSensorListener,
				mSensor, 3);
		Log.d(TAG, "flag-------->" + flag);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mSensorManager.unregisterListener(mSensorListener);
	}

	private SensorManager mSensorManager;
	private Sensor mSensor;
	private float mValues[];
	int m_nCurArrow;
	int m_rotation;
	TextView ms_tv_XYZ[];

	/*
	 * static float[] access$002(MotionSenorTest motionsenortest, float af[]) {
	 * motionsenortest.mValues = af; return af; }
	 */

	SensorEventListener mSensorListener = new SensorEventListener() {
		
		public void onSensorChanged(SensorEvent event) {
			// onSensorEvent(event.values[0], event.values[1], event.values[2]);
			Log.d(TAG, "x--->"+"11111111111");
			TextView textview = ms_tv_XYZ[0];
			StringBuilder stringbuilder = (new StringBuilder()).append("X = ");
			float f = event.values[0];
			String s = stringbuilder.append(f).toString();
			textview.setText(s);
			
			TextView textview1 = ms_tv_XYZ[1];
			StringBuilder stringbuilder1 = (new StringBuilder()).append("Y = ");
			float f1 = event.values[1];
			String s1 = stringbuilder1.append(f1).toString();
			textview1.setText(s1);
			
			TextView textview2 = ms_tv_XYZ[2];
			StringBuilder stringbuilder2 = (new StringBuilder()).append("Z = ");
			float f2 = event.values[2];
			String s2 = stringbuilder2.append(f2).toString();
			textview2.setText(s2);
			Log.d(TAG, "4-------->"+Math.sqrt(4));
			
			StringBuilder stringbuilder3 = (new StringBuilder()).append("Φ = ");
			StringBuilder stringbuilder4 = (new StringBuilder()).append("ρ = ");
			Φ =Math.atan(f/(Math.sqrt(f1*f1+f2*f2)));
			ρ = Math.atan(f1/(Math.sqrt(f*f+f2*f2)));
			String s3 = stringbuilder3.append(Φ).toString();
			String s4 = stringbuilder4.append(ρ).toString();
			ms_tv_XYZ[3].setText(s3);
			ms_tv_XYZ[4].setText(s4);
			Log.d(TAG, "x--->" + f + "\n" + "y--->" + f1 + "\n" + "x--->"
					+ f2);
		}

		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// ignore
		}
	};

}
